﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WxPayAPI
{
    public class H5ApiPay
    {
        /// <summary>
        /// 统一下单接口返回结果
        /// </summary>
        public WxPayData unifiedOrderResult { get; set; }


        /// <summary>
        /// 商品金额，用于统一下单
        /// </summary>
        public int total_fee { get; set; }

        /// <summary>
        /// 场景信息WAP网站应用
        ////{"h5_info": //h5支付固定传"h5_info" 
        ////   {"type": "",  //场景类型
        ////    "wap_url": "",//WAP网站URL地址
        ////    "wap_name": ""  //WAP 网站名
        ////    }
        ////}
        /// </summary>
        public string scene_info { get; set; }

        /**
         * 调用统一下单，获得下单结果
         * @return 统一下单结果
         * @失败时抛异常WxPayException
         */
        public WxPayData GetUnifiedOrderResult(string OrderID)
        {
            //统一下单
            WxPayData data = new WxPayData();
            data.SetValue("body", "商品购买");
            data.SetValue("attach", OrderID);
            data.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo());
            data.SetValue("notify_url", "https://www.rdkhyc.com/WxPayAPI/ResultNotify.aspx");
            data.SetValue("total_fee", total_fee);
            data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));
            data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"));
            data.SetValue("goods_tag", "商品购买");
            data.SetValue("trade_type", "MWEB");
            data.SetValue("scene_info", scene_info);


            WxPayData result = WxPayApi.UnifiedOrder(data);
            if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString() == "")
            {
                Log.Error(this.GetType().ToString(), "UnifiedOrder response error!");
                throw new WxPayException("UnifiedOrder response error!");
            }
            Log.Error(this.GetType().ToString(), "【微信H5支付】统一下单成功：" + result.ToJson());

            unifiedOrderResult = result;
            return result;
        }

        /**
       *  
       * 从统一下单成功返回的数据中获取微信浏览器调起jsapi支付所需的参数，
       * 微信浏览器调起JSAPI时的输入参数格式如下：
       * {
       *   "appId" : "wx2421b1c4370ec43b",     //公众号名称，由商户传入     
       *   "timeStamp":" 1395712654",         //时间戳，自1970年以来的秒数     
       *   "nonceStr" : "e61463f8efa94090b1f366cccfbbb444", //随机串     
       *   "package" : "prepay_id=u802345jgfjsdfgsdg888",     
       *   "signType" : "MD5",         //微信签名方式:    
       *   "paySign" : "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名 
       * }
       * @return string 微信浏览器调起JSAPI时的输入参数，json格式可以直接做参数用
       * 更详细的说明请参考网页端调起支付API：http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7
       * 
       */
        public string GetH5ApiParameters()
        {
            //Log.Debug(this.GetType().ToString(), "JsApiPay::GetJsApiParam is processing...");

            WxPayData jsApiParam = new WxPayData();
            jsApiParam.SetValue("appId", unifiedOrderResult.GetValue("appid"));
            jsApiParam.SetValue("timeStamp", WxPayApi.GenerateTimeStamp());
            jsApiParam.SetValue("nonceStr", WxPayApi.GenerateNonceStr());
            jsApiParam.SetValue("package", "prepay_id=" + unifiedOrderResult.GetValue("prepay_id"));
            jsApiParam.SetValue("signType", "MD5");
            jsApiParam.SetValue("paySign", jsApiParam.MakeSign());

            string parameters = jsApiParam.ToJson();

            //Log.Debug(this.GetType().ToString(), "Get jsApiParam : " + parameters);
            return parameters;
        }


    }
}